package com.atguigu.cloud.controller;

import cn.hutool.core.date.DateUtil;
import com.atguigu.cloud.apis.PayFeignApi;
import com.atguigu.cloud.entities.PayDTO;
import com.atguigu.cloud.resp.ResultData;
import com.atguigu.cloud.resp.ReturnCodeEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.servers.Server;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/feign")
public class ConsumerFeignController {

    @Resource
    PayFeignApi payFeignApi;

    @PostMapping("/pay/add")
    public ResultData<String> addOrder(@RequestBody PayDTO payDTO) {
        System.out.println("第一步：模拟本地addOrder新增订单成功(省略sql操作)，第二步：再开启addPay支付微服务远程调用");
        return payFeignApi.addPay(payDTO);
    }

    @GetMapping("/pay/del/{id}")
    public ResultData<Integer> getPayInfo(@PathVariable("id") Integer id) {
        System.out.println("-------支付微服务远程调用，按照id查询订单支付流水信息");
        return payFeignApi.deletePay(id);
    }

    @GetMapping(value = "/pay/get/{id}")
    public ResultData<PayDTO> getById(@PathVariable("id") Integer id) {
        ResultData<PayDTO> resultData = null;
        try {
            System.out.println("调用开始："+DateUtil.now());
            resultData = payFeignApi.getById(id);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("调用结束:"+DateUtil.now());
            return ResultData.fail(ReturnCodeEnum.RC500.getCode(), e.getMessage());
        }
       return resultData;
    }

    /**
     * openfeign天然支持负载均衡演示
     */
    @GetMapping(value = "/pay/get/info")
    public String getInfoByConsul() {
        try {
            log.info("Feign调用开始时间"+ DateUtil.now());
            return payFeignApi.getInfoByConsul();
        }catch (Exception e){

            log.info("Feign调用结束时间"+DateUtil.now());
        }
        return null;
    }
}
